/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.crab.web;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.kaaction.common.web.BaseController;
import com.kaaction.modules.crab.entity.MsgHistroy;
import com.kaaction.modules.crab.entity.Order;
import com.kaaction.modules.crab.service.MsgHistroyService;
import com.kaaction.modules.crab.service.OrderService;
import com.kaaction.modules.crab.utils.SingleSendSms;

/**
 * 信息发送Controller
 * @author huanghaiming
 * @version 2017-07-21
 */
public class SendMessageController extends BaseController{

	@Autowired 
	OrderService orderService;
	
	@Autowired 
	MsgHistroyService msgHistroyService;
	
	public void sendMessage(){
		List<Order> orders = orderService.selOrders();
		if(orders!=null && orders.size()>0){
			MsgHistroy msgHistroy  = new MsgHistroy();
			SingleSendSms app = null;
			for(Order order:orders){
				 String orderNum= order.getOrderNum();
				 String expressNum= order.getExpressNum();
				 app = new SingleSendSms();
				 String str = app.sendMsg(order.getMobile(), "{'orderNo':'"+orderNum+"','expresNo':'"+expressNum+"'}");
				 if("false".equals(str)){
					 order.setMsgState("2");
					 msgHistroy.setState("1");
				 }else{
					 order.setMsgState("1");
					 msgHistroy.setState("0");
				 }
				 int os = orderService.updateState(order);
				 logger.info("os="+os);
				 if(os>0){
					 msgHistroy.setCreateDate(new Date());
					 msgHistroy.setMobile(order.getMobile());
					 msgHistroy.setContent("【蟹壹品大闸蟹】 您的订单"+orderNum+"阳澄湖大闸蟹礼盒已经从苏州基地发货，注意保持手机畅通，顺丰单号"+expressNum+"，请注意查收，售后服务电话4000-181-775。");
					 msgHistroy.setOrderId(order.getId());
					 msgHistroyService.insert(msgHistroy);
				 }
			}
			
		}
	}
	

}